package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Family;
import com.domain.Jzlx;
import com.service.FamilyService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Controller
public class FamilyController {
	
	@Autowired
	FamilyService familyService;

	@RequestMapping("/family.do")
	String toFamily(@RequestParam(value="hzxm", defaultValue="")String hzxm,
			@RequestParam(value="pageNum", defaultValue="1")int pageNum,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize, 
			HttpServletRequest request ,Model model){
		
		
		List<Map<String,Object>> family= new ArrayList<Map<String,Object>>();
		int count = 0;
		Map<String,Object> params = new HashMap<String,Object>();
		String ocode = (String)request.getSession().getAttribute("ocode");
		if(ocode.equals("0")){
			ocode="";
		}
		params.put("hzxm", hzxm);
		params.put("pageBegin", (pageNum-1)*pageSize);
		params.put("pageSize", pageSize);
		params.put("ocode", ocode);
		
		family = familyService.getFamily(params);
		count = familyService.getCount(params);
		params.put("totalCount", count);
		
		
		model.addAttribute("params",params);
		model.addAttribute("family", family);
		return "family/family";
		
	}
	
	@RequestMapping("toUpdateFamily.do")
	public String toUpdateFamily(@RequestParam(value="fid") int fid,Model model){
		Map<String,Object> family= new HashMap<String,Object>();
		family = familyService.getPomrByUid(fid);
		model.addAttribute("family", family);
		return "family/showFamily";
	}
	
	@RequestMapping("doUpdateFamily.do")
	public void doUpdateFamily(@RequestParam("fid") int fid,
			@RequestParam("fcode") String fcode,
			@RequestParam("hzxm") String hzxm,
			@RequestParam("zfmc") String zfmc,
			@RequestParam("jtrs") int jtrs,
			@RequestParam("fdlrs") int fdlrs,
			@RequestParam("ysmc") String ysmc,
			@RequestParam("cjrq") String cjrq,
			@RequestParam("oname") String oname,PrintWriter out
			){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("更新成功！");
		try {
			Family family = new Family();
			family.setFid(fid);
			family.setFcode(fcode);
			family.setFdlrs(fdlrs);
			family.setJtrs(jtrs);
			family.setHzxm(hzxm);
			family.setCjrq(ObjectUtils.getDateVal(cjrq));
			family.setHzid(familyService.getHzid(hzxm));
			family.setZflx(familyService.getZflx(zfmc));
			family.setFzys(familyService.getFzys(ysmc));
			family.setJdjg(familyService.getJdjg(oname));
			
			familyService.updateFamily(family);
			
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("更新失败！");
		}		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
	@RequestMapping("toAddFamily.do")
	public String toAddFamily(Model model){
		
		List<Jzlx> listZjlx = familyService.getListZjlx();
		model.addAttribute("dmZjlx", listZjlx);
		return "family/toaddfamily";
	}
	
	@RequestMapping("doAddFamily.do")
	public void doAddFamily(@RequestParam("fcode")String fcode,
			@RequestParam("hzxm")String hzxm,
			@RequestParam("zjlx")int zjlx,
			@RequestParam("jtrs")int jtrs,
			@RequestParam("fdlrs")int fdlrs,
			@RequestParam("ysmc")String ysmc, PrintWriter out,HttpSession session){
		
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("更新成功！");
		try {
			Family family = new Family();
			family.setFcode(fcode);
			family.setCjrq(new Date());
			family.setFdlrs(fdlrs);
			family.setHzxm(hzxm);
			family.setHzid(familyService.getHzid(hzxm));
			family.setJdjg(ObjectUtils.getIntVal((String) session.getAttribute("ocode")));
			family.setJtrs(jtrs);
			family.setZflx(zjlx);
			family.setFzys(familyService.getFzys(ysmc));
			
			familyService.addFamily(family);
			
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("更新失败！");
		}
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
		
	}
	
	@RequestMapping("deleteFamily.do")
	public void deleteFamily(@RequestParam("fid") int fid,PrintWriter out){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("删除成功！");
		try {
			familyService.deleteFamily(fid);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("删除失败！");
		}
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
}
